
from homeassistant.const import (ATTR_ENTITY_ID,ATTR_FRIENDLY_NAME,ATTR_DOMAIN)

from .const import (
    SET_COLOR ,
    NULL_VALUE ,
    SET_COLOR_TEMPERATURE ,
    SET_BRIGHTNESS 
)

def get_props(cur_dev,dev):
        """返回开关属性"""
        if dev['name'] == 'power':
            dev['value']  = cur_dev['state']
        elif dev['name'] == 'color':
            dev['value'] = cur_dev['attributes'][SET_COLOR]  if SET_COLOR in cur_dev['attributes'] else NULL_VALUE
        elif dev['name'] == SET_COLOR_TEMPERATURE:
            dev['value'] = cur_dev['attributes'][SET_COLOR_TEMPERATURE] if SET_COLOR_TEMPERATURE in cur_dev['attributes'] else NULL_VALUE
        elif dev['name'] == SET_BRIGHTNESS:
            dev['value'] = cur_dev['attributes'][SET_BRIGHTNESS] / 255 *100  if SET_BRIGHTNESS in cur_dev['attributes'] else NULL_VALUE
        else:
            dev['value'] = NULL_VALUE
        return dev


def set_props_to_call_service_data(cur_dev,dev):
    """设置开关"""
    domain = cur_dev[ATTR_ENTITY_ID].split('.')[0]
    
    service = ''
    data = {ATTR_ENTITY_ID:cur_dev[ATTR_ENTITY_ID]}
    if dev['name'] == 'power':
        service = 'turn_on' if dev['value'] == 'on' else 'turn_off'
    elif dev['name'] == 'color':
        data[SET_COLOR]=dev['value']
        service = 'turn_on'

    elif dev['name'] == SET_COLOR_TEMPERATURE:
        service = 'turn_on'
        data[SET_COLOR_TEMPERATURE]=dev['value']

    elif dev['name'] == SET_BRIGHTNESS:
        service = 'turn_on'
        data[SET_BRIGHTNESS]=dev['value']

    return [domain,service,data]